package com.lmx.project.until;

import com.lmx.project.common.ErrorCode;
import com.lmx.project.exception.BusinessException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.io.*;

@Component
public class FileUtilAsync {
    @Value("${myfile.pre}")
    private String pre;

//    @Value("${myfile.preurl}")
//    private String preurl;

    /**
     * 将输入流保存到指定路径的文件中
     *
     * @param inputStream 输入流
     * @param path        文件保存的路径，包括文件名
     * @throws IOException 如果写入文件时发生I/O错误，则会抛出IOException
     */
    @Async
    public void saveFileFromInputStream(InputStream inputStream, String path) throws IOException {
        //            byte[] bytes = file.getBytes();
        //压缩图片到指定120K以内,不管你的图片有多少兆,都不会超过120kb,精度还算可以,不会模糊
////        压缩图片
//        ByteArrayOutputStream output = new ByteArrayOutputStream();
//        byte[] bufferome = new byte[1024 * 4];
//        int n = 0;
//        while (-1 != (n = inputStream.read(bufferome))) {
//            output.write(bufferome, 0, n);
//        }
//        byte[] bytes = PicUtils.compressPicForScale(output.toByteArray(), 600);
//        inputStream = new ByteArrayInputStream(bytes);
//        读取文件
        File file = new File(pre + path);
        System.out.println(file.getParentFile());
        if (file.getParentFile() != null && !file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        FileOutputStream outputStream = new FileOutputStream(file);
        byte[] buffer = new byte[1024];
        int len;
        while ((len = inputStream.read(buffer)) > 0) {
            outputStream.write(buffer, 0, len);
        }
        outputStream.close();
        inputStream.close();
        System.out.println("保存文件成功：" + path);
    }


    /**
     * 删除文件
     */
    @Async
    public void removeFile(String path) {
        String realpath = pre + path;
        System.out.println(realpath);
        File file = new File(realpath);

//     如果存在，删除它
        boolean delete = file.delete();
        if (!delete) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "图片删除失败，请重试");
        }

    }

    /**
     * 保存文件，绝对路径
     */
    @Async
    public void saveFile(InputStream inputStream, String absolutePath) throws IOException {
        absolutePath=pre+absolutePath;
        System.out.println(absolutePath+"开始保存");
        File file = new File(absolutePath);

        if (file.getParentFile() != null && !file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }

        FileOutputStream outputStream = new FileOutputStream(file);

        byte[] buffer = new byte[1024];
        int len;
        while ((len = inputStream.read(buffer)) > 0) {
            outputStream.write(buffer, 0, len);
        }

        inputStream.close();
        outputStream.close();
        System.out.println(absolutePath+"保存完毕");

    }

    public String getPre() {
        return pre;
    }
}
